const app = getApp()
import { getSupplierMessageList, setSupplierMessageRead } from '../../../../api/index'
import { formatTimeTwo } from '../../../../utils/util'

Page({
  data: {
    tabs: [
      { label: '全部消息', value: 0, key: 'allTypeCount', allTypeCount: 0 },
      { label: '系统消息', value: 1, key: 'systemCount', systemCount: 0 },
      { label: '消息公告', value: 5, key: 'announcementCount', announcementCount: 0 },
      { label: '入册消息', value: 2, key: 'bookCount', bookCount: 0 },
      { label: '样品消息', value: 3, key: 'orderCount', orderCount: 0 }
    ],
    tabActive: 0,
    isEmpty: false,
    currentPage: 1,
    pageSize: 15,
    list: [],
    currentItem: {},
    showMsgModal: false
  },
  selectTab ({ currentTarget: { dataset: { item } } }) {
    this.setData({ tabActive: item.value })
    this.getList()
  },
  async seeDetail ({ currentTarget: { dataset: { item, index } } }) {
    try {
      if (item.status === 2) {
        let query = { messageId: item.messageId }
        if (item.type === 5) query.type = 1
        await setSupplierMessageRead({ ...query })
        this.data.list[index].status = item.status === 2 ? 1 : 2
        let key = `list[${index}].status`
        this.setData({ [key]: this.data.list[index].status })
        if (item.type === 5) {
          wx.$nav.navigateTo(`/supplier/pages/message/detail/index?id=${item.messageId}`)
        } else {
          this.setData({ showMsgModal: true, currentItem: item })
        }
      } else {
        if (item.type === 5) {
          wx.$nav.navigateTo(`/supplier/pages/message/detail/index?id=${item.messageId}&type=${item.type}`)
        } else {
          this.setData({ showMsgModal: true, currentItem: item })
        }
      }
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  close () {
    this.setData({ showMsgModal: false, currentItem: {} })
  },
  async loadMore () {
    try {
      if (this.loading || this.alloaded) return
      this.loading = true
      const { ResultSet } = await getSupplierMessageList({ currentPage: this.data.currentPage, pageLines: this.data.pageSize, type: this.data.tabActive })
      this.alloaded = !ResultSet.userData.messageList.length
      this.data.isEmpty = !ResultSet.userData.messageList.length
      this.setData({ isEmpty: this.data.isEmpty })
      if (ResultSet.userData.messageList.length) {
        this.data.currentPage = this.data.currentPage + 1
        this.data.list.push(...ResultSet.userData.messageList)
        this.setData({
          currentPage: this.data.currentPage,
          list: this.data.list
        })
        // this.setList(ResultSet.userData.messageList)
      }
      this.loading = false
    } catch (err) {
      console.log(err)
      this.loading = false
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  setList (arr) {
    arr.forEach(item => {
      item.createTime = formatTimeTwo(item.createTime, 'Y-M-D h:m:s')
    })
    this.data.list.push(...arr)
    this.setData({ list: this.data.list })
  },
  async getList () {
    try {
      this.setData({ list: [] })
      let query = {
        currentPage: 1,
        pageLines: this.data.pageSize,
        type: this.data.tabActive
      }
      wx.$pro.showLoading({ title: '加载中...', mask: true })
      const { ResultSet } = await getSupplierMessageList({ ...query })
      if (this.data.tabActive === 0) {
        this.data.tabs.forEach(item => {
          item[item.key] = ResultSet.userData[item.key]
        })
        this.setData({ tabs: this.data.tabs })
      }
      this.loading = false
      this.alloaded = false
      if (ResultSet.userData.messageList.length) this.data.currentPage = 2
      this.data.isEmpty = ResultSet.userData.messageList.length && ResultSet.userData.messageList.length < this.data.pageSize
      this.setData({
        currentPage: this.data.currentPage,
        isEmpty: this.data.isEmpty,
        list: ResultSet.userData.messageList
      })
      // this.setList(ResultSet.userData.messageList)
      this.loading = false
      wx.$pro.hideLoading()
    } catch (err) {
      wx.$pro.hideLoading()
      this.loading = false
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  onLoad () {
    this.getList()
  }
})